package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.SysPost;
import org.example.erp.domain.UserLoginLog;
import org.example.erp.service.impl.SysPostServiceImpl;
import org.example.erp.service.impl.SysUserPostServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/api")
@RestController
public class PostController {

    @Autowired
    SysPostServiceImpl sysPostService;//岗位列表

    @Autowired
    SysUserPostServiceImpl userPostService;//用户岗位表

    @GetMapping("/postManagement/find")//查询岗位列表（分页，带参）
    public DataResult findLoginLog(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String postCode,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String postName
    ) {
        LambdaQueryWrapper<SysPost> postMapper = new LambdaQueryWrapper<>();
        IPage<SysPost> page = new Page<>(currentPage, pageSize);
        //链式调用,前面是表达式，判断条件是否成立
        postMapper.like(!"".equals(postCode) && postCode != null, SysPost::getPostCode, postCode)
                .like(!"".equals(postName) && postName != null, SysPost::getPostName, postName)
                .eq(!"".equals(status) && status != null, SysPost::getStatus, status)
                .orderByAsc(SysPost::getPostSort); // 按 postSort 升序排序
        IPage<SysPost> pageResult = sysPostService.page(page, postMapper);
        return DataResult.success(pageResult);
    }

    @PostMapping("/postManagement/findBySys_postId")//查询岗位是否分配用户
    public DataResult findBySys_postId(@RequestBody List<Integer> ids){
        int bySysPostId = userPostService.findBySys_postId(ids);
        return DataResult.success(bySysPostId);
    }

    @PostMapping("/postManagement/insert")//添加岗位
    public DataResult insertPost(@RequestBody SysPost sysPost) {
        sysPost.setCreateTime(new Date());
        boolean save = sysPostService.save(sysPost);
        if (save) {
            return DataResult.success(save ? "成功" : "失败");
        } else{
            return DataResult.error("岗位新增失败");
        }
    }

    @PutMapping("/postManagement/update")//修改岗位
    public DataResult updatePost(@RequestBody SysPost sysPost){
        sysPost.setUpdateTime(new Date());
        boolean b = sysPostService.updateById(sysPost);
        if(b){
            return DataResult.success(b ? "成功" : "失败");
        } else{
            return DataResult.error("岗位更新失败");
        }
    }

    @DeleteMapping("/postManagement/delete")//多选删除
    public DataResult deletePostIds(@RequestBody List<Long> postId){
        sysPostService.removeBatchByIds(postId);
        return DataResult.success();
    }

//    @DeleteMapping("/postManagement/delete")//单行删除
//    public DataResult deletePostId(@RequestParam Integer postId){
//        sysPostService.removeById(postId);
//        return DataResult.success();
//    }
}
